package util;

import javafx.application.Application;
import javafx.stage.Stage;

import java.sql.*;

/**
 * 获取  mysql 连接
 * 关闭连接
 */
public class DatabaseUtil {

    private static String driver =ConfigManager.getProperty("driver");
    private static String url =ConfigManager.getProperty("url");
    private static String username =ConfigManager.getProperty("username");
    private static String password =ConfigManager.getProperty("password");

    static {
        try {
            Class.forName(driver);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取数据库连接
     * @return
     * @throws SQLException
     */
    public static Connection getConnection() throws SQLException {
        Connection conn = null;
        try {
            if (null == conn || conn.isClosed())
            conn= DriverManager.getConnection(url,username,password);

        } catch (SQLException throwables) {
            throwables.printStackTrace();
            throw  throwables;
        }
        return  conn;
    }
    public static void close(Connection conn , Statement stmt , ResultSet rs){
        try {
            if(null!=rs){
                rs.close();
            }
            if(null!=stmt){
                stmt.close();
            }
            if(null!=conn){
                conn.close();
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

}
